﻿using Entitas;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// if the collison entity is a projectile Ie. a bow collision entity then based on the direction we must move the entity across the screen
/// too find a target.
/// </summary>
class ProjectileCollisionMovementSystem : ReactiveSystem<CollisionEntity>
{
    CollisionContext collisionContext;
    public ProjectileCollisionMovementSystem(Contexts contexts) : base(contexts.collision)
    {
        collisionContext = contexts.collision;
    }
    protected override void Execute(List<CollisionEntity> entities)
    {
        foreach (var e in collisionContext.GetEntities().Where(x => x.isProjectile == true))
        {
            var pos = e.position.value;
            switch (e.direction.value)
            {

                case Direction.Up:
                    pos.y += e.speed.value;
                    e.ReplacePosition(pos);
                    break;
                case Direction.Down:
                    pos.y -= e.speed.value;
                    e.ReplacePosition(pos);
                    break;
                case Direction.Left:
                    pos.x -= e.speed.value;
                    e.ReplacePosition(pos);
                    break;
                case Direction.Right:
                    pos.x += e.speed.value;
                    e.ReplacePosition(pos);
                    break;
                default:
                    break;
            }
        }
    }

    protected override bool Filter(CollisionEntity entity)
    {
        return entity.isProjectile;
    }

    protected override ICollector<CollisionEntity> GetTrigger(IContext<CollisionEntity> context)
    {
        var triggers = new TriggerOnEvent<CollisionEntity>[2];
        triggers[0] = CollisionMatcher.Projectile.Added();
        triggers[1] = CollisionMatcher.Position.AddedOrRemoved();
        return context.CreateCollector(triggers);
    }
}


